package com.app.time;

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {
		//
		private static String url="http://10.0.2.2:8080/TimeWeb/login.action?";
		private final String url_constant="http://10.0.2.2:8080/TimeWeb/login.action?";
		private EditText uname;
		private EditText upass;
		private Button btnLogin;
		private Button btnRegist;
		@Override
		public void onCreate(Bundle savedInstanceState) {
			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()   
			.detectDiskReads()   
			.detectDiskWrites()   
			.detectNetwork()   // or .detectAll() for all detectable problems   
			.penaltyLog()   
			.build());   
			StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()   
			.detectLeakedSqlLiteObjects()   
			//.detectLeakedClosableObjects()   
			.penaltyLog()   
			.penaltyDeath()   
			.build());
			super.onCreate(savedInstanceState);
			setContentView(R.layout.login);
			initView();
			setListener();
		}

		private void initView() {
			btnLogin = (Button)findViewById(R.id.btnLogin);
			btnRegist = (Button)findViewById(R.id.btnRegist);
			uname = (EditText)findViewById(R.id.uname);
			upass = (EditText)findViewById(R.id.upass);
		}
		private void setListener() {
			btnLogin.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					String uname = LoginActivity.this.uname.getText().toString();
					Log.v("uname = ", uname);
					String upass = LoginActivity.this.upass.getText().toString();
					Log.v("upass = ", upass);
					loginRemoteService(uname,upass);
				}
			});
			btnRegist.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(LoginActivity.this,RegistActivity.class);
					startActivity(intent);
				}
			});
		}

		public void loginRemoteService(String uname,String upass){
			String result=null;
			try {
				HttpClient httpclient = new DefaultHttpClient();
				//processURL=processURL+"userName="+userName+"&password="+password;
				url= url_constant+"uname="+uname+"&upass="+upass;
				Log.d("Զ��URL", url);
				HttpGet request = new HttpGet(url);
				request.addHeader("Accept","text/json");
				HttpResponse response = httpclient.execute(request);//??
				HttpEntity entity = response.getEntity();
				String json = EntityUtils.toString(entity,"UTF-8");
				if(json != null){
					JSONObject jsonObject=new JSONObject(json);
					result=jsonObject.get("message").toString();
					if (result.equals("用户合法")) {
						Intent intent = new Intent(LoginActivity.this,Success.class);
						startActivity(intent);
					}else{
						Log.d("message", "用户名或密码错误");
					}
				}else{
					json="网络不通";
					Log.d("message", "登录失败");
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
}
